/*
  Copyright (c) 2009-10 by kRaw1er & HierOS
  This file is part of M8J project
*/
#pragma once

#include <mzfc_inc.h>
#include <CallNotifyApi.h>
#include <mzfc\UiList_H.h>
#include <mzfc\UiToolbar_Icon.h> 
#include <mzfc\MzWnd.h>
#include <mzfc\uibutton_image.h>

#include "utils\utils.h"
#include "typedefs.h"
#include "CChatWnd.h"
#include "COptions.h"
#include "CLoginWnd.h"
#include "COptionsWnd.h"
#include "CSettingsWnd.h"
#include "CContactList.h"
#include "CChangeStatusWnd.h"
#include "gloox\client.h"
#include "gloox\rostermanager.h"
#include "gloox\connectionlistener.h"
#include "gloox\connectiontcpbase.h"
#include "gloox\gloox.h"
#include "gloox\adhoc.h"

using namespace gloox;

#define DEFAULT_NUMBER_OF_CONNECT_ATTEMPTS				3

#define MZ_IDC_TIMER									1
#define TIMER_TIMEOUT									1000

#define MZ_IDC_CONTACT_LIST								100
#define MZ_IDC_MAIN_BAR									101
#define MZ_IDC_CHANGE_STATUS_BUTTON						102
#define MZ_IDC_SETTINGS_BUTTON							103
#define MZ_IDC_PROFILE_BUTTON							104
#define MZ_IDC_TOOLBARICON								105
#define MZ_IDC_CAPTION									203
#define MZ_IDC_CAPTION2									204

#define APP_NAME L"M8Jabber"

class CM8JMainWnd : public CMzWndEx, public LogHandler, public ConnectionListener
{
	MZ_DECLARE_DYNAMIC(CM8JMainWnd);
public:
	ImagingHelper m_ImgChangeStatusNormal;
	ImagingHelper m_ImgChangeStatusPressed;
	UiButton_Image m_ChangeStatusButton;
	UiButton_Image m_SettingsButton;
	UiButton m_ProfileButton;
	CContactList m_ContactList;
	Client* jClient;
	Adhoc* jAdhoc;
	UiButton_Image m_CaptionStatus;

	CChatWnd m_ChatWnd;
	COptions m_Options;

	CM8JMainWnd();

	~CM8JMainWnd();


	//*******************************************************************************
	// CMzWndEx functions
	virtual BOOL OnInitDialog();
	virtual void OnMzCommand(WPARAM wParam, LPARAM lParam);
	virtual LRESULT MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam);
	virtual void OnTimer(UINT_PTR nIDEvent);
	//*******************************************************************************

	//*******************************************************************************
	// Log handler
	virtual void handleLog(LogLevel level, LogArea area, const std::string &message);
	//*******************************************************************************

	//*******************************************************************************
	// Connection listener
	virtual void onConnect ();
	virtual void onDisconnect (ConnectionError e);
	virtual bool onTLSConnect (const CertInfo &info);
	//*******************************************************************************

	void DialGPRS();
	void OnInternetConnected();
	void OnInternetDisconnected();
	void OnChangeNetworkStatus(DWORD new_network_status);
	void DisconnectToXMPP();
	void ConnectToXMPP(const std::string &username, const std::string &password, const std::string &server, const std::string &resource, Presence presence = PresenceAvailable, const std::string &presence_message = "");
	static GPRSSTATUSCALLBACK OnGprsStatus(DWORD dwGprsStatus);
	void SendMessage(const std::string &jid, const std::wstring &message);
	void RemoveAllMessages();
	void RemoveJidMessages(const std::string &jid);
	void SetPresence(Presence presence);
	void SetPresence(Presence presence, const std::string &msg);

private:
	static CM8JMainWnd * m_This;
	bool m_IsDialGPRS;
	bool m_XMPPConnected;
	bool m_InternetAvailable;
	bool m_ReconnectIfLineDropped;
	bool m_ConnectingToXMPP;
	bool m_ErrorOccured;
	int m_NumberOfConnectAttempts;
	int m_NumberOfConnectAttemptsLeft;
	//JID m_JID;
	std::string m_Username;
	std::string m_Password;
	std::string m_Server;
	std::string m_Resource;
	Presence m_Presence;
	std::string m_PresenceMessage;
	DWORD m_NetworkStatus;
	UiToolbar_Icon m_ToolbarIcon;
 
	void ConnectToXMPP();
	int ShowChatWnd(const std::string &jid, Messages * pMessages);
};
